package taskit.storage;

import java.util.Collection;
import java.util.TreeSet;

import org.codehaus.jackson.annotate.JsonIgnore;

import taskit.test.Log;
import taskit.datastructures.Deadline;
import taskit.datastructures.Schedule;

/*
 * This TreeSet records the type of tasks found on a particular date
 */
//@author A0098733R
public class MyTreeSet extends TreeSet<Deadline> {
	Data.TaskTypes types;
	private int numberOfDeadlines;
	private int numberOfSchedules;

	public MyTreeSet() {
		super();
		types = Data.TaskTypes.HAS_NONE;
		numberOfDeadlines = 0;
		numberOfSchedules = 0;
	}

	@Override
	public boolean add(Deadline deadline) {
		boolean value = super.add(deadline);
		if (value) {
			if (deadline instanceof Schedule) {
				numberOfSchedules++;
			} else {
				numberOfDeadlines++;
			}
			updateTypes();
		}
		return value;
	}

	@Override
	public boolean addAll(Collection<? extends Deadline> list) {
		boolean value = true;
		for (Deadline deadline : list) {
			value &= add(deadline);
		}
		return value;
	}
	
	@Override
	public boolean remove(Object o) {
		boolean returnValue = super.remove(o);
		if (returnValue) {
			if (o instanceof Schedule) {
				numberOfSchedules--;
			} else {
				numberOfDeadlines--;
			}
			updateTypes();
		}
		return returnValue;
	}
	
	private void updateTypes() {
		
		if (numberOfDeadlines == 0 && numberOfSchedules == 0) {
			types = Data.TaskTypes.HAS_NONE;
		} else if (numberOfDeadlines == 0) {
			types = Data.TaskTypes.HAS_SCHEDULE;
		} else if (numberOfSchedules == 0) {
			types = Data.TaskTypes.HAS_DEADLINE;
		} else {
			types = Data.TaskTypes.HAS_DEADLINE_AND_SCHEDULE;
		}
	}

	@JsonIgnore
	public Data.TaskTypes getTaskTypes() {
		if (types == null) {
			return Data.TaskTypes.HAS_NONE;
		} else {
			return types;
		}
	}
}
